﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Furion.Application
{
    /// <summary>
    /// 字典 Dictionary
    /// </summary>
    [AppAuthorize, ApiDescriptionSettings("System", Name = "Dictionary", Order = 2, Tag = "字典Dictionary")]
    public class DictionaryService : IDynamicApiController
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IRepository<Furion.Core.Dictionary> _dictionaryRepository;
        private readonly IAuthorizationManager _authorizationManager;



        public DictionaryService(IHttpContextAccessor httpContextAccessor,
            IRepository<Furion.Core.Dictionary> dictionaryRepository,
            IAuthorizationManager authorizationManager)
        {
            _httpContextAccessor = httpContextAccessor;
            _dictionaryRepository = dictionaryRepository;
            _authorizationManager = authorizationManager;
        }


        /// <summary>
        /// 新增 Dictionary
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Add), HttpPost("/Dictionary/Add"), ApiDescriptionSettings(Name = "Add")]
        public async Task<int> AddAsync([Required] DictionaryAddInput input)
        {
            var addInput = input.Adapt<Dictionary>();
            var entryEntity = await _dictionaryRepository.InsertNowAsync(addInput);

            return entryEntity.Entity.Id;
        }


        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Edit), HttpPost("/Dictionary/Edit"), ApiDescriptionSettings(Name = "Edit")]
        public async Task EditAsync([Required] DictionaryEditInput input)
        {
            var editInput = input.Adapt<Dictionary>();

            await _dictionaryRepository.UpdateAsync(editInput);
        }


        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/Dictionary/PageList"), ApiDescriptionSettings(Name = "PageList")]
        public async Task<PagedList<Dictionary>> PageListAsync([FromQuery] DictionaryPageInput input)
        {
            var hasKeyword = !string.IsNullOrEmpty(input.Keyword?.Trim());
            var list = await _dictionaryRepository
                .Where(a => a.IsDeleted == input.IsDeleted)
                .Where(hasKeyword, a => a.Name.Contains(input.Keyword.Trim()))
                .OrderBy(a => a.Id)
                .ToPagedListAsync(input.PageIndex, input.PageSize);

            return list.Adapt<PagedList<Dictionary>>();
        }

        /// <summary>
        /// 详细信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_List), HttpGet("/Dictionary/Detail"), ApiDescriptionSettings(Name = "Detail")]
        public async Task<Dictionary> DetailAsync([FromQuery, Required] long Id)
        {
            var info = await _dictionaryRepository.Where(a => a.Id.Equals(Id)).FirstOrDefaultAsync();

            return info;

        }


        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Delete), HttpPost("/Dictionary/Delete"), ApiDescriptionSettings(Name = "Delete")]
        public async Task DeleteRange(DictionaryDeleteRangeInput input)
        {
             

            await _dictionaryRepository.Entities.Where(b => input.Ids.Contains(b.Id)).ExecuteUpdateAsync(s => s.SetProperty(b => b.IsDeleted, b => true));
        }

        /// <summary>
        /// 删除后恢复
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [SecurityDefine(ConstPermission.Dictionary_Recovery), HttpPost("/Dictionary/Recovery"), ApiDescriptionSettings(Name = "Recovery")]
        public async Task RecoveryRange(DictionaryDeleteRangeInput input)
        {
            await _dictionaryRepository.Entities.Where(b => input.Ids.Contains(b.Id)).ExecuteUpdateAsync(s => s.SetProperty(b => b.IsDeleted, b => false));
        }



    }
}
